package edu.msg.jthr.web.servlets;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.Department;
import edu.msg.jthr.backend.model.Job;
import edu.msg.jthr.backend.model.User;
import edu.msg.jthr.backend.service.DepartmentService;
import edu.msg.jthr.backend.service.JobService;
import edu.msg.jthr.backend.service.UserService;
import edu.msg.jthr.web.util.RoleAssembler;

/**
 * Servlet implementation class UpdateJob
 */
@WebServlet("/UpdateJob")
public class UpdateJob extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory
			.getLogger(AddCandidate.class);
	@EJB
	private DepartmentService departmentService;
	@EJB
	private JobService jobService;
	@EJB
	private UserService userService;
	private HttpSession session;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateJob() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		User user = (User) session.getAttribute("activeUser");
		if (user == null) {
			user = userService.getUserByUserName(request
					.getUserPrincipal().toString());
			String role = RoleAssembler.getRole(user.getUserGroups());
			session.setAttribute("role", role);
			session.setAttribute("activeUser", user);
		}
		String pagePath = "/ShowJobs";
		request.getRequestDispatcher(pagePath).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Exception handling
		session = request.getSession(true);
		User user = (User) session.getAttribute("activeUser");
		Job job = (Job) session.getAttribute("job");
		job.setProposer(user);
		job.setTitle(request.getParameter("title"));
		job.setMaxCandidates(Integer.parseInt(request
				.getParameter("candidates")));
		job.setOpened(Boolean.valueOf(request.getParameter("opened")));
		job.setApproved(Boolean.valueOf(request.getParameter("approved")));
		try {
			String dateString = request.getParameter("date");
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date date;
			date = formatter.parse(dateString);
			job.setDeadLine(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Department department = departmentService.getById(Long
				.parseLong(request.getParameter("selection")));
		job.getDescription().setDepartment(department);
		job.getDescription().setProject(request.getParameter("project"));
		job.getDescription().setRequirements(
				request.getParameter("requirements"));
		job.getDescription().setResponsabilities(
				request.getParameter("responsabilities"));
		jobService.update(job);

		List<Job> jobList = Collections.emptyList();
		try {
			jobList = jobService.getAll();
		} catch (ServiceException e) {
			LOG.error(e.getMessage());
		}
		session.setAttribute("jobList", jobList);
		request.getRequestDispatcher("/pages/ShowJobs.jsp").forward(request,
				response);
	}

}
